import { Dispatch } from "redux";
import { IResBody, MyAxios } from "../../../../common/config/http";
import { GET_LIST, SET_TABLE_LOADING } from "./types";
import { IAction } from "../../../../common/store/actions";


const setTableLoading = (loading: boolean): IAction => ({
  type: SET_TABLE_LOADING,
  data: loading
});

export const getList = () => {
  return (dispatch: Dispatch) => {
    dispatch(setTableLoading(true));
    MyAxios.axios
      .post("user/list", null, { headers: { ...MyAxios.getToken() } })
      .then(res => {
        const resData: IResBody = res.data;
        const action: IAction = {
          type: GET_LIST,
          data: resData
        };
        dispatch(setTableLoading(false));
        dispatch(action);
      })
      .catch(e => {
        dispatch(setTableLoading(false));
      });
  };
};
